﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class Explode:CharDrawAction
    {
        private Timer timer = Core.CreateTimer();
        private Sprite explosion = Sprites.CreateSprite(Resources.LoadBitmap("explosion.png"), 5, 4, 1200, 800);
        private Sprite safeSpot = Sprites.CreateSprite(Resources.LoadBitmap("safespot.png"), 2, 4, 283, 283);
        private bool isPlayer;
        private int activeDur, explodeTime, minDamage;

        public Explode(Character c, bool persistant, int row, int colEnd, int count, int lockoutLevel, int minDamage, 
            int explodeTime, int activeDur, bool isPlayer)
            : base(c, persistant, row, colEnd, count, lockoutLevel)
        {
            this.isPlayer = isPlayer;
            this.minDamage = minDamage;
            this.activeDur = activeDur;
            this.explodeTime = explodeTime;
        }

        public override void DoAction(GameController gc)
        {
            if (Core.TimerTicks(timer) == 0 && !IsActive)
            {
                Core.StartTimer(timer);
                IsActive = true;
                explosion.CurrentCell = 0;
                safeSpot.X = Core.Rnd(GlobalConst.ScreenWidth - safeSpot.Width);
                safeSpot.Y = 90 + Core.Rnd(GlobalConst.ScreenHeight - safeSpot.Height - 90);
            }

            if (Core.TimerTicks(timer) > (explodeTime + activeDur) && IsActive)
                Reset(gc);
            else if (Core.TimerTicks(timer) > explodeTime && Core.TimerTicks(timer) < (explodeTime+activeDur))
                foreach (DestroyableObject obj in gc.GameStages.Peek().CharList)
                    if (obj is Character && ((obj is Player) == isPlayer) &&
                        !Physics.SpritesCollided(obj.ObjectSprite, safeSpot) && Physics.SpritesCollided(obj.ObjectSprite, explosion))
                    {
                        obj.TakeDamage(Core.Rnd(minDamage) + minDamage, gc);
                        Console.WriteLine(obj.Name);
                    }

                        
        }

        public override void Draw()
        {
            if (Core.TimerTicks(timer) < (explodeTime + activeDur))
            {
                explosion.Draw();
                safeSpot.Draw();
                safeSpot.UpdateAnimation();
            }
            if (Core.TimerTicks(timer) > explodeTime && Core.TimerTicks(timer) < (explodeTime + activeDur))
                explosion.UpdateAnimation();
        }

        public override void Reset(GameController gc)
        {
            Core.StopTimer(timer);
            IsActive = false;
        }
    }
}
